<?php
require_once 'includes/auth.php';
require_once 'includes/config.php';
requireLogin();

$username = $_SESSION['username'];

// 检查是否已经是开发者
if (getDeveloperInfo($username)) {
    header('Location: index.php');
    exit;
}
require_once 'track_online.php';
// 处理注册请求
$error = '';
if ($_SERVER['REQUEST_METHOD'] === 'POST') {
    $reason = trim($_POST['reason'] ?? '');
    
    if (strlen($reason) < 10) {
        $error = "请提供更详细的理由（至少10个字符）";
    } else {
        registerDeveloper($username, $reason);
        header('Location: index.php');
        exit;
    }
}
?>
<!DOCTYPE html>
<html>
<head>
    <title>注册开发者 - JIIL微聊</title>
    <link rel="stylesheet" href="css/style.css">
    <style>
        .dev-form {
            max-width: 600px;
            margin: 0 auto;
            background: white;
            padding: 30px;
            border-radius: 5px;
            box-shadow: 0 2px 5px rgba(0,0,0,0.1);
        }
        .form-group {
            margin-bottom: 20px;
        }
        textarea {
            width: 100%;
            height: 150px;
            padding: 10px;
            border: 1px solid #ddd;
            border-radius: 4px;
        }
    </style>
</head>
<body>
    <div class="top-bar">
        <h1>JIIL微聊开发平台</h1>
        <div class="nav-links">
            <a href="index.php">返回首页</a>
        </div>
    </div>
    
    <div class="container">
        <h2>注册开发者账号</h2>
        
        <?php if ($error): ?>
            <div style="color: red; padding: 10px; background: #ffecec; margin-bottom: 20px;">
                <?= $error ?>
            </div>
        <?php endif; ?>
        
        <div class="dev-form">
            <p>成为开发者后，您将获得API调用权限，可用于发送自动消息</p>
            
            <form method="POST">
                <div class="form-group">
                    <label>申请理由:</label>
                    <textarea name="reason" required placeholder="请说明为什么需要开发者权限..."></textarea>
                </div>
                <button type="submit" class="btn">提交申请</button>
            </form>
        </div>
    </div>
</body>
</html>